package com.datastructure.test.findword;

import java.util.ArrayList;
import java.util.Arrays;

public class FindWord {

    public static void main(String[] args) {
        FindWord findWord = new FindWord();
        ArrayList<Integer> word = findWord.findWord("ababab", "ab");
        System.out.println(word);
    }

    public ArrayList<Integer> findWord (String s, String p) {
        // write code here
        int pLength = p.length();
        int sLength = s.length();
        if(sLength<pLength){
            return null;
        }
        ArrayList<Integer> resultArrayList = new ArrayList<>();
        char[] pCharArray = p.toCharArray();
        String tempString;
        Arrays.sort(pCharArray);
        p = new String(pCharArray);
        for (int i = 0; i < sLength-pLength+1; i++) {
                tempString=s.substring(i,i+pLength);
                if(judge(tempString,p)){
                    resultArrayList.add(i);
                }
        }
        return resultArrayList;
    }

    private boolean judge(String tempString, String p) {
        char[] charArray = tempString.toCharArray();
        Arrays.sort(charArray);
        tempString=new String(charArray);
        return tempString.contains(p);
    }
}
